Imports System.Drawing

''' <summary>
''' This is the base class for all jChart charts.
''' </summary>
''' <remarks></remarks>
Public MustInherit Class Chart
    Protected Friend moChartDimensions As ChartDimensions

    Private msUniqueName As String
    Private msFileName As String

    ''' <summary>
    ''' Create a new instance of a chart and initialize the chart dimensions object which
    ''' objects belonging to this chart must use.
    ''' </summary>
    ''' <param name="iWidth">Width of chart image</param>
    ''' <param name="iHeight">Height of chart image</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal iWidth As Integer, ByVal iHeight As Integer)
        moChartDimensions = New ChartDimensions(iWidth, iHeight)

        Dim oGuid As New Guid
        oGuid = Guid.NewGuid()

        msUniqueName = oGuid.ToString
    End Sub

    ''' <summary>
    ''' This is the filename that will is used for the image when it is saved to disk. 
    ''' It should not include the path. If a filename is not provided prior to the SaveChartToFile 
    ''' method being used, then a unique filename will be automatically generated.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FileName() As String
        Get
            Return msFileName
        End Get
        Set(ByVal value As String)
            msFileName = value
        End Set
    End Property

    ''' <summary>
    ''' The ChartDimensions property exposes the internal layout of the chart including size of areas 
    ''' reserved for X and Y axis labels, margin height and width, and various background colors.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ChartDimensions() As ChartDimensions
        Get
            Return moChartDimensions
        End Get
        Set(ByVal value As ChartDimensions)
            moChartDimensions = value
        End Set
    End Property

    ''' <summary>
    ''' The DrawChart method draws the chart on a graphic.
    ''' </summary>
    ''' <param name="g"></param>
    ''' <remarks></remarks>
    Public MustOverride Sub DrawChart(ByVal g As Graphics)

    ''' <summary>
    '''  The SaveChartToFile method saves the chart to a file. If a value has been provided for the 
    ''' FileName property then the chart is saved to that filename, otherwise a unique filename 
    ''' is created. If a unique filename has been created then that filename may be retrieved via the FileName property.
    ''' The format of the created image can be any one of the available System.Drawing.Imaging.ImageFormat 
    ''' image types including JPEG, GIF, BMP, and TIFF. Graphics are saved using 24-bit color depth. 
    ''' </summary>
    ''' <param name="sPath">Physical file location where image should be stored</param>
    ''' <param name="iType">Type of image (JPG, GIF, etc.)</param>
    ''' <remarks></remarks>
    Public Sub SaveChartToFile( _
                    ByVal sPath As String, _
                    ByVal iType As System.Drawing.Imaging.ImageFormat)

        If msFileName = "" Then
            msFileName = msUniqueName & "." & iType.ToString
        End If

        Dim oPicture As New Bitmap( _
                                moChartDimensions.OuterWidth, _
                                moChartDimensions.OuterHeight, _
                                System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        DrawChart(Graphics.FromImage(oPicture))

        Try
            oPicture.Save(sPath & msFileName, iType)

        Catch oEX As System.Runtime.InteropServices.ExternalException
            Dim oChartEx As New ChartException("Unable to create chart image. Directory may not exist or process may lack required permissions to directory.")
            Throw oChartEx
        End Try
    End Sub
End Class
